package com.msb.eurekaserver;


import com.netflix.appinfo.InstanceInfo;
import com.netflix.discovery.EurekaClient;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;

@RestController
public class MainController2 {

    @Autowired
    DiscoveryClient discoveryClient;

    @Autowired
    LoadBalancerClient LBClient3;

    @Autowired
    RestTemplate restTemplate;

    @GetMapping("/client6")
    public Object client6(){

        //Ribbon完成客户端的负载均衡，过滤down下线了的节点
        ServiceInstance instance = LBClient3.choose("provider");
        String url = "http://"+instance.getHost()+":"+instance.getPort()+"/getHi";
        System.out.println("url: "+url);
        String resStr = restTemplate.getForObject(url, String.class);
        System.out.println("resStr: "+resStr);
        return resStr;
    }
    @GetMapping("/client7")
    public Object client7() {
        List<ServiceInstance> instances = discoveryClient.getInstances("provider");
        // 自定义轮训算法

        // 随机
        int nextInt = new Random().nextInt(instances.size());
        AtomicInteger atomicInteger = new AtomicInteger();
        // 轮训
        int i = atomicInteger.getAndIncrement();
        instances.get(i % instances.size());
        // 权重。。
        for (ServiceInstance serviceInstance : instances) {
            //	int quanzhong = 	serviceInstance.getMetadata(); // 权重  1-9
        }
        ServiceInstance instance = instances.get(nextInt);

        // ribbon 完成客户端的负载均衡，过滤掉down了的节点
        //	ServiceInstance instance = lb.choose("provider");

        String url ="http://" + instance.getHost() +":"+ instance.getPort() + "/getHi";
        String respStr = restTemplate.getForObject(url, String.class);
        System.out.println("resStr: "+respStr);
        return respStr;
    }

    @GetMapping("/client8")
    public Object client8(){

        //Ribbon完成客户端的负载均衡，过滤down下线了的节点
        ServiceInstance instance = LBClient3.choose("provider");
        String url = "http://"+instance.getHost()+":"+instance.getPort()+"/getHi";
        System.out.println("url: "+url);
        String resStr = new RestTemplate().getForObject(url, String.class);
        System.out.println("resStr: "+resStr);
        return resStr;
    }

    @GetMapping("/client9")
    public Object client9(){
        String url = "http://provider/getHi";
        String resStr = restTemplate.getForObject(url, String.class);
        System.out.println("resStr: "+resStr);
        return resStr;
    }
}
